package com.ktjiaoyu.ktmall.dao.impl;

import com.ktjiaoyu.ktmall.dao.BaseDao;
import com.ktjiaoyu.ktmall.dao.CategoryDao;
import com.ktjiaoyu.ktmall.entity.Category;
import com.mysql.cj.x.protobuf.MysqlxExpr;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CategoryDaoImpl extends BaseDao implements CategoryDao {

    @Override
    public List<Category> findAll() {
        String sql="select `id`, `name`, `parent_id`, `icon`, `sort`, `status`, `is_deleted`, `update_time`  from  kt_category where is_deleted=0 order by parent_id,sort asc";
        try {
            return executeQuery(sql,rs->{

                    List<Category> list=new ArrayList<Category>();
                    try {
                    while (rs.next()){
                        list.add(extractCategory(rs));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return list;
            });
        } catch (SQLException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }

    }

    @Override
    public int delete(Long id) {
        String sql="UPDATE kt_category SET is_deleted=1 WHERE id=?";
        try{
            return executeUpdate(sql,id);
        }catch (SQLException e){
            e.printStackTrace();
            return -1;
        }

    }

    @Override
    public int insert(Category category) {
        System.out.println(category.getParentId());
        String sql="INSERT INTO kt_category(name,parent_id,icon,sort,status) VALUES(?,?,?,?,?)";
        try{
            return executeUpdate(sql,category.getName(),category.getParentId(),category.getIcon(),category.getSort(),category.getStatus());
        }catch (SQLException e){
            e.printStackTrace();
            return -1;
        }


    }

    @Override
    public int update(Category category) {
        StringBuilder sql=new StringBuilder("UPDATE kt_category SET");
        List<Object> params=new ArrayList<>();
        if (category.getName()!=null){
            sql.append(" name=?,");
            params.add(category.getName());
        }
        if (category.getParentId()!=null){
            sql.append(" parent_id=?,");
            params.add(category.getParentId());
        }
        if (category.getIcon()!=null){
            sql.append(" icon=?,");
            params.add(category.getIcon());
        }
        if (category.getSort()!=null){
            sql.append(" sort=?,");
            params.add(category.getSort());
        }
        if (category.getStatus()!=null){
            sql.append(" status=?,");
            params.add(category.getStatus());
;        }
        sql.append(" update_time=NOW() WHERE id=?");
        params.add(category.getId());
        try{
            return executeUpdate(sql.toString(),params.toArray());
        }catch (SQLException e){
            e.printStackTrace();
            return -1;
        }
    }

    private Category extractCategory(ResultSet rs) throws SQLException {
        Category cat = new Category();
        cat.setId(rs.getString("id"));
        cat.setName(rs.getString("name"));
        cat.setParentId(rs.getString("parent_id"));
        cat.setIcon(rs.getString("icon"));
        cat.setSort(rs.getInt("sort"));
        cat.setStatus(rs.getInt("status"));
        cat.setIsDeleted(rs.getInt("is_deleted"));
        return cat;
    }
}
